<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.9.1.min.js"></script>
		<script src="js/json2.js"></script>
		<script>
			$(document).ready(function(){
				$("#showAllUserBtn").click(function(){
					$("#step1").show();
				});
				
				$("#loginBtn").click(function(){
					var url = "/SpringDemo/oauth/token?client_id=my-trusted-client&grant_type=password&scope=read,write,trust&username=" + $("#userName").val() + "&password=" +$("#password").val(); 
					$.ajax({
						type: "GET",
						url: url,
						dataType : 'json',
						contentType:"application/json",
						success: function(data, textStatus) {
							if (data && data.access_token) {
								$("#step2").show();
								$("#step2").html("<br/>↓<br/><br/>认证成功：" + JSON.stringify(data));
								
								$("#step3").show();
								var step3Str = "<br/>↓<br/><br/><a href='/SpringDemo/user/list?access_token=" + data.access_token + "'>点我获得内容</a>";
								$("#step3").html(step3Str);
							} else {
								alert("access_token取得失败！请输入admin账号密码");
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							alert("access_token取得失败！ - " + errorThrown);
						}
					});
				});
			});
		</script>
	</head>
	<body>
		<h1>REST + JSON 接口测试</h1> 
		<form action="j_spring_security_check" method="post"> 
			<p>账号：<input type="text" name="j_username" value="admin"/></p> 
		   	<p>密码：<input type="password" name="j_password" value="admin"/></p>  
		  	<p><input type="submit" value="提交"/></p>
		</form>
		<p>
			接口一览
			<ul>
				<li>添加商品：/good/add - POST</li>
				<li>商品一览：/good/list - GET</li>
				<li>所有商品：/good/listAll - GET</li>
				<li>所有用户：/user/list - GET</li>
			</ul>
		</p>
		<hr/>
		<h1>OAth2 测试</h1>
		<div>
			<input id="showAllUserBtn" type="button" value="查看所有用户"/>
		</div>
		<div id="step1" style="display:none;">
			<br/>↓<br/>
			<form action="" method="post"> 
					<p>账号：<input type="text" id="userName" name="userName" value="admin"/></p> 
			   	<p>密码：<input type="password" id="password" name="password" value="admin"/></p>  
			  	<p><input id="loginBtn" type="button" value="确定"/></p>
			</form>
		</div>
		<div id="step2" style="display:none;">
		</div>
		<div id="step3" style="display:none;">
		</div>
	</body>
</html>
